package cc.customer.cRound.service.Impl;

import cc.customer.cRound.dao.CRoundConfigMapper;
import cc.customer.cRound.dao.CRoundGradeMapper;
import cc.customer.cRound.domain.CRound;
import cc.customer.cRound.domain.CRoundConfig;
import cc.customer.cRound.domain.CRoundGrade;
import cc.customer.cRound.service.CRoundService;
import cc.customer.common.domain.ResponseBo;
import cc.customer.common.service.impl.BaseService;
import cc.customer.common.utils.DateUtil;
import cc.customer.common.utils.Utils;
import cc.customer.system.domain.Dept;
import cc.customer.system.service.DeptService;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class CRoundServiceImpl extends BaseService<CRound> implements CRoundService {


    @Autowired
    private CRoundConfigMapper configMapper;

    @Autowired
    private CRoundGradeMapper gradeMapper;

    @Autowired
    private DeptService deptService;

    @Override
    public ResponseBo addNewRound(CRound round){
        try {
            Example example1= new Example(CRound.class);
            example1.createCriteria().andEqualTo("rName",round.getrName());
            List<CRound> lsit = this.selectByExample(example1);
            if(!lsit.isEmpty()){return ResponseBo.error("已经存在"+round.getrName()+"项目");}

            String uuid = Utils.getUUID();
            round.setrUuid(uuid);
            round.setrStatus("1");
            round.setrType("1");
            round.setrCreateTime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
            int num = this.save(round);
            if(num>0){
                Example example = new Example(CRound.class);
                example.createCriteria().andEqualTo("rUuid",uuid);
                round = this.selectByExample(example).get(0);
                return ResponseBo.ok("保存成功",round);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseBo.error("保存失败");
    }


    @Override
    public ResponseBo selcetAllRound(CRound round){
        try {
            Example example = new Example(CRound.class);
            example.createCriteria().andEqualTo("rType",round.getrType());
            List<CRound> list = this.selectByExample(example);
            return ResponseBo.ok("",list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseBo.error("查询失败");
    }


    @Override
    public ResponseBo deleteRound(CRound round){
        try {
            int num  = this.delete(round);//this.delete(uuids);
            if (num>0){
                return ResponseBo.ok("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseBo.error("删除失败");
    }

    @Override
    public ResponseBo getRoundConfig(CRoundConfig roundConfig){
        try {
            List<CRoundConfig> list = configMapper.selectConfigByPid(roundConfig);
            List<CRoundConfig> returnlist = new ArrayList<>();
            for (CRoundConfig roundConfig1:list) {
                String deptids = roundConfig1.getcDeptid();
                if (!StringUtils.isEmpty(deptids)){
                    CRoundConfig  cRoundConfig = null;
                    List<String> depts = Arrays.asList(deptids.split(","));
                    for (String dept:depts){
                        Dept dept1 = deptService.findById(Long.valueOf(dept));
                        cRoundConfig = new CRoundConfig();
                        cRoundConfig.setcUuid(roundConfig1.getcUuid());
                        cRoundConfig.setcDeptid(dept1.getDeptId()+"");
                        cRoundConfig.setcDeptName(dept1.getDeptName());
                        returnlist.add(cRoundConfig);
                    }
                }
            }

            List<CRoundConfig> gradeList = configMapper.selectGradeByPid(roundConfig.getcPid());

            JSONObject json = new JSONObject();
            json.put("config",returnlist);
            json.put("grade",gradeList);
            return ResponseBo.ok("查询成功",json);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseBo.error("查询失败");
    }
}
